<!--
 * @Description: 系统参数
 * @Author: wangwangwang
 * @Date: 2020-06-29 11:18:28
 * @LastEditors: wangwangwang
 * @LastEditTime: 2021-04-14 16:34:21
-->
<template>
  <div class="pages_content dictionary-container">
    <el-container>
      <el-header>
        <breaDcrumb />
      </el-header>
      <el-main>
        <div class="header_right" @click="showCardNav = !showCardNav">
          <div class="title">搜索</div>
          <div>
            <div :class="{ 'arrowTransform': !showCardNav, 'arrowTransformReturn': showCardNav}">
              <i style="margin-right: 5px" class="el-icon-arrow-down"></i>
            </div>
          </div>
        </div>
        <div v-show="showCardNav" class="hr"></div>
        <el-collapse-transition>
          <div v-show="showCardNav">
            <div class="search">
              <el-form ref="form" :inline="true" :model="form">
                <el-form-item label="名称" prop="paramName">
                  <el-input v-model="form.paramName" placeholder="请输入" @keyup.enter.native="initData()"></el-input>
                </el-form-item>
                <el-form-item label="编码" prop="code">
                  <el-input v-model="form.code" placeholder="请输入" @keyup.enter.native="initData()"></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button icon="el-icon-search" size="medium" class="bottom_all" @click="initData()">查 询</el-button>
                  <el-button v-if=" ButtonList.findIndex(res=>(res.buttonValue=='CHONGZHI'))!==-1" size="medium" class=" bottom_all_two" @click="resetData()">重 置</el-button>
                </el-form-item>
              </el-form>
            </div>
          </div>
        </el-collapse-transition>
        <div class="hr"></div>
        <div class="card">
          <div class="card_nav">
            <div>
              <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='TIANJIA'))!==-1" size="small" class="bottom_all" @click="add()">
                <i class="icon iconfont iconicon_button_add" style="font-size: 12px;"></i>
                添加
              </el-button>
              <!-- <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='SHANCHU'))!==-1" size="small" class="bottom_two" @click="updateType()">
                <i class="icon iconfont iconicon_button_delete" style="font-size: 12px;"></i>
                删除
              </el-button> -->
              <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='TONGBUHC'))!==-1" icon="el-icon-check" size="small" class="bottom_three" @click="syncCache()">同步缓存</el-button>
            </div>
            <div>
              <el-dropdown :hide-on-click="false" trigger="click" style="margin: 0 20px;">
                <el-link :underline="false">
                  <span class="el-icon--left">
                    <i class="iconfont iconicon_button_filtrate"></i>
                  </span>
                  筛选
                </el-link>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item v-for="(item, index) in tableColumns" :key="index">
                    <el-checkbox v-model="item.status">{{ item.label }}</el-checkbox>
                  </el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
              <el-link v-if="ButtonList.findIndex(res=>(res.buttonValue=='DAOCHU'))!==-1" :underline="false" @click="$Download(DownloadURL, form)">
                <span class="el-icon--left">
                  <i class="iconfont iconicon_button_export1"></i>
                </span>
                导出
              </el-link>
            </div>
          </div>
          <div class="card_info" :style="style">
            <el-table :header-cell-style="{ background: '#f3f6f9' }" :cell-style="{ borderRight: 'none' }" border stripe style="width:100%" :data="tableData" tooltip-effect="dark" @selection-change="handleSelectionChange">
              <el-table-column align="center" type="selection"></el-table-column>
              <template v-for="(item, index) in tableColumns">
                <el-table-column v-if="item.status" :key="index" :label="item.label" align="center">
                  <template slot-scope="scope">
                    <span v-if="scope.row.enable == 0 && item.label == '状态'" style="color:#969799;">禁用</span>
                    <span v-if="scope.row.enable == 1 && item.label == '状态'" style="color:#00A754;">启用</span>
                    <el-button v-if="item.label == '操作'&&ButtonList.findIndex(res=>(res.buttonValue=='XIUGAI'))!==-1" class="bottom_one" size="mini" @click="update(scope.row)">
                      <i class="icon iconfont iconicon_button_modification" style="font-size: 12px;"></i>
                      修改
                    </el-button>
                    <span v-if="item.label !== '操作' && item.label !== '状态'">{{ scope.row[item.prop] }}</span>
                  </template>
                </el-table-column>
              </template>
            </el-table>
          </div>
        </div>
      </el-main>
      <div class="card_bottom">
        <div class="card_bottom_left">
          <div class="top_page_totle">总共{{ total }}条</div>
          <el-pagination :page-size="pageSize" :page-sizes="[10, 20, 30, 40,50,100,200,500,1000]" layout="prev,pager,sizes,next,jumper" :total="total" :current-page.sync="page" @size-change="sizeChange" @current-change="initData()"></el-pagination>
        </div>
        <div>
          <el-button size="mini" icon="el-icon-refresh-right" class="bottom_all_two" @click="initData()">刷新</el-button>
        </div>
      </div>
    </el-container>
    <!-- 添加 -->
    <el-dialog v-dialogDrag :visible.sync="dialogVisible" width="650px" :close-on-click-modal="false" :before-close="resefrom">
      <span slot="title" class="dialog-title">{{ dialogtitle }}</span>

      <el-form ref="refdialogForm" :rules="rules" :model="dialogForm" label-width="80px">
        <el-form-item prop="code">
          <span slot="label" style="color:#FF0000">编码：</span>
          <el-input v-model="dialogForm.code" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item prop="paramName">
          <span slot="label" style="color:#FF0000">名称：</span>
          <el-input v-model="dialogForm.paramName" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="参数值" prop="paramValue">
          <el-input v-model="dialogForm.paramValue" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="dialogForm.remark" placeholder="备注"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button class="bottom_all_two" @click="resefrom()">取 消</el-button>
        <el-button class="bottom_all" @click="sureSubmit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { queryPage, saveOrUpdate, updateOrDelete, Download, sync_cache } from "@/api/basedata/system";
import breaDcrumb from "@/components/breadcrumb";
import { mapGetters } from "vuex";
export default {
  components: {
    breaDcrumb,
  },
  data() {
    return {
      DownloadURL: Download,
      /*添加修改弹窗*/
      dialogVisible: false,
      loading: false,
      dialogtitle: "添加数据字典",
      /*判断收起展开*/
      showCardNav: true,
      /*列表展示*/
      tableColumns: [
        { label: "编码", width: 120, prop: "code", status: true },
        { label: "名称", width: 120, prop: "paramName", status: true },
        { label: "参数值", width: 120, prop: "paramValue", status: true },
        { label: "备注", width: 120, prop: "remark", status: true },
        { label: "操作", width: 120, prop: "", status: true },
      ],
      /*列表选中的数据集*/
      SelectionTableData: [],
      tableData: [],
      total: 0,
      dialogTable: [],
      page: 1,
      pageSize: 10,
      dialogForm: {
        code: "",
        paramName: "",
        paramValue: "",
        remark: "",
      },
      rules: {
        paramName: [{ required: true, message: "请输入名称", trigger: "blur" }],
        code: [{ required: true, message: "请输入编码", trigger: "blur" }],
        paramValue: [
          { required: true, message: "请输入参数值", trigger: "blur" },
        ],
        remark: [{ required: true, message: "请输入备注", trigger: "blur" }],
      },
      form: {
        code: "",
        paramName: "",
      },
      ButtonList: []
    };
  },
  mounted() {
    this.ButtonList = this.$ButtonList()
    this.initData();
  },
  methods: {
    syncCache() {
      sync_cache().then(res => {
        if (res.code == 1001) {
          this.$message({
            message: "同步成功！",
            type: "success",
          });
        }
      })
    },
    resefrom() {
      this.dialogVisible = false;
      this.$refs.refdialogForm.resetFields();
    },
    initData() {
      let params = {
        ...this.form,
        page: this.page,
        pageSize: this.pageSize,
      };
      console.log(params);
      this.loading = true;
      queryPage(params).then((res) => {
        this.loading = false;
        this.tableData = res.data.data;
      });
    },
    resetData() {
      this.$refs.form.resetFields();
      this.initData();
    },
    add() {
      this.dialogVisible = true;
      this.$data.dialogForm = this.$options.data().dialogForm;
      setTimeout(() => {
        this.$refs.refdialogForm.clearValidate();
      }, 10);
      this.dialogtitle = "添加系统参数";
    },
    updateType() {
      if (!this.SelectionTableData.length) {
        this.$message.warning('请选择要删除的数据');
        return;
      }
      const self = this;
      this.$confirm("确定删除选中的数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        cancelButtonClass: "bottom_all_two",
        confirmButtonClass: "bottom_all",
        type: "warning",
      }).then(() => {
        let params = {
          status: 0,
          type: "update_status",
        };
        self.handleData(params);
      });
    },
    handleData(data) {
      const params = {
        ...data,
        ids: this.SelectionTableData,
      };
      updateOrDelete(params).then((res) => {
        console.log(res);
        this.initData();
        this.$message({
          message: "删除成功！",
          type: "success",
        });
      });
    },
    update(data) {
      /**
       * @Description: 修改系统参数
       * @Param:
       * @Author: wangwangwang
       */
      this.dialogVisible = true;
      this.dialogtitle = "修改系统参数";
      this.dialogForm = JSON.parse(JSON.stringify(data));
    },
    handleSelectionChange(val) {
      const data = [];
      for (let i in val) {
        data.push(val[i].id);
      }
      this.SelectionTableData = data;
    },
    sureSubmit() {
      this.$refs.refdialogForm.validate((valid) => {
        if (valid) {
          const params = {
            ...this.dialogForm,
          };
          saveOrUpdate(params).then((res) => {
            if (res.code === 1001) {
              this.$message({
                message: "操作成功！",
                type: "success",
              });
              this.initData();
              this.resefrom();
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    sizeChange(value) {
      this.pageSize = value;
      this.initData();
    },
  },
  computed: {
    ...mapGetters({
      screenWidth: "getscreenWidth",
      screenHeight: "getscreenHeight",
      Collapse: "getsCollapse",
    }),
    style() {
      let num = 0;
      if (this.Collapse) {
        num = this.screenWidth - 124;
      } else {
        num = this.screenWidth - 220;
      }
      return "width:" + num + "px;";
    },
  },
};
</script>
<style lang="scss" scoped>
.dictionary-container {
  .el-link {
    i {
      font-size: 14px;
      color: $bg_color;
    }
  }
  .el-link:hover {
    color: $bg_color;
  }
  font-size: $font_size;
  .el-main {
    font-size: 14px;
    padding: 0;
  }
  .el-main::-webkit-scrollbar {
    width: 3px;
    height: 3px;
  }
  .el-main::-webkit-scrollbar-track {
    background: rgb(239, 239, 239);
    border-radius: 2px;
  }
  .el-main::-webkit-scrollbar-thumb {
    background: #bfbfbf;
    border-radius: 10px;
  }
  .el-main::-webkit-scrollbar-thumb:hover {
    background: #333;
  }
  .el-main::-webkit-scrollbar-corner {
    background: #179a16;
  }
  .search {
    padding: 20px;
  }
  .card_nav {
    padding: 0 20px;
    height: 76px;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
  .card_info {
    padding: 0 20px;
  }
  .header_right {
    @include discen_al;
    padding: 0 20px;
    justify-content: space-between;
    .title {
      font-size: 16px;
      font-family: PingFang SC;
      font-weight: bold;
    }
  }
  .card_bottom {
    padding: 0 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 72px;
    .card_bottom_left {
      display: flex;
      align-items: center;
      font-size: $font_size;
    }
  }
}
</style>
